package com.ruoyi.system.controller;

import com.ruoyi.common.core.controller.BaseController;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.*;

/**
 * swagger 用户测试方法
 *
 * @author ruoyi
 */
@Api("通用方法接口")
@RestController
@RequestMapping("/test/")
public class TransController extends BaseController
{
    @ApiOperation(value = "根据sql文件，获取指定的数据生成到一个新的文件")
    @RequestMapping("/generateSql")
    public void generateSql()
    {
        String inputFilePath = "C:\\Users\\cm199\\Desktop\\test\\test\\新建文件夹\\draw_graph_obj.sql"; // 输入的大SQL文件路径
        String outputDirPath = "C:\\Users\\cm199\\Desktop\\test\\test\\demo\\"; // 输出的小SQL文件目录
        int linesPerFile = 20000; // 每个小文件的行数
        String targetString = "0848106f41b74084b2b2e8184a45e055"; // 目标字符串

        try (BufferedReader reader = new BufferedReader(new FileReader(inputFilePath))) {
            String line;
            int fileNumber = 1;
            int lineCount = 0;
            BufferedWriter writer = null;

            while ((line = reader.readLine()) != null) {
                if (line.contains(targetString)) { // 判断行是否包含目标字符串
                    if (lineCount % linesPerFile == 0) {
                        if (writer != null) {
                            writer.close();
                        }
                        String outputFilePath = outputDirPath + "part_" + fileNumber + ".sql";
                        writer = new BufferedWriter(new FileWriter(outputFilePath));
                        fileNumber++;
                    }
                    writer.write(line);
                    writer.newLine();
                    lineCount++;
                }
            }

            if (writer != null) {
                writer.close();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

